<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .xiaomi {
            width: 512px;
            height: 400px;
            border: 1px solid #f00;
            margin: 100px auto;
            overflow: hidden;
            position: relative;
        }

        .xiaomi span {
            position: absolute;
            left: 0;
            width: 100%;
            height: 200px;
            cursor: pointer;
        }

        .up {
            top: 0;
        }

        .down {
            bottom: 0;
        }

        .xiaomi img {
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
<div class="xiaomi" id="xiaomi">
    <img src="mi.png" alt="" id="pic"/>
    <span class="up" id="goUp"></span>
    <span class="down" id="goDown"></span>
</div>
<script>
    var goUp = document.getElementById("goUp");
    var goDown = document.getElementById("goDown");
    var pic = document.getElementById("pic");
    var xiaomi = document.getElementById("xiaomi");

    var y = 0;
    var timerId = null;//用来接收定时器的id
    goUp.onmouseover = function () {
        clearInterval(timerId);
        timerId = setInterval(function () {
            var picMaxY = pic.offsetHeight - xiaomi.offsetHeight;
            if (y > -picMaxY) {
                y -= 5;
                pic.style.top = y + "px";
            } else {
                clearInterval(timerId);
            }
        }, 15);
    }
    goDown.onmouseover = function () {
        clearInterval(timerId);
        timerId = setInterval(function () {
            if (y < 0) {
                y += 5;
                pic.style.top = y + "px";
            } else {
                clearInterval(timerId);
            }

        }, 15);
    }

    xiaomi.onmouseout = function () {
        clearInterval(timerId);
    }
</script>
</body>
</html>